package org.jeecg.modules.qyxx.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanUtils;
import org.jeecg.common.util.Rest;
import org.jeecg.modules.qyxx.entity.req.BaseRequest;
import org.jeecg.modules.qyxx.entity.req.KfptAppQyxxQueryRequest;
import org.jeecg.modules.qyxx.entity.req.KfptDoServiceRequest;
import org.jeecg.modules.qyxx.entity.resp.BaseResponse;
import org.jeecg.modules.qyxx.service.IGetSessionService;
import org.jeecg.modules.qyxx.service.NsrService;
import org.jeecg.modules.qyxx.service.NsrzxService;
import org.jeecg.modules.qyxx.util.BszsCommonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.security.auth.login.LoginException;

@Slf4j
@Service
public class NsrzxServiceImpl implements NsrzxService {

    private String loginSid = "";

    private String nsrxxSid = "";

    @Autowired
    private IGetSessionService getSessionService;
    /*@Autowired
    private GeneralQueryService generalQueryService;

    @Autowired
    private IGetSessionService getSessionService;*/
    @Autowired
    private BszsCommonUtils bszsCommonUtils;
    @Autowired
    private NsrService nsrService;

   //查询纳税人主要信息
    /* @Override
    public Rest<BaseResponse<String>> nsrzyxxcx(NsrzyxxcxRequest request) {
        Rest rest = new Rest();
        try {
            String requestXml = IOUtils.toString(this.getClass().getResourceAsStream("/xml/nsrzyxxcx.xml"), "UTF-8")
                    .replace("$djxh", request.getDjxh());
            String nsrsbh = request.getNsrsbh();
            Rest<Boolean> responseRest = getSessionService.LoginByNsrsbh(nsrsbh);// 登录
            if (!responseRest.getData()) {
                throw new RuntimeException(responseRest.getMessage());
            }
            KfptSwyAppBaseRequest kfptSwyAppBaseRequest = new KfptSwyAppBaseRequest();
            kfptSwyAppBaseRequest.setContent(requestXml);
            kfptSwyAppBaseRequest.setNsrsbh(nsrsbh);
            bszsCommonUtils.setKfptDoServiceRequestFromRedis(kfptSwyAppBaseRequest, nsrsbh);//设置redis
            com.foresee.tax.kfpt.client.response.BaseResponse baseResponse = generalQueryService.getRestByContentXml(kfptSwyAppBaseRequest, "XML", "nsrzyxxcx");
            if (baseResponse != null && baseResponse.getData() != null) {
                rest.setData(XmlUtil.xml2Json(baseResponse.getData().toString()));
                return rest;
            }
            if (baseResponse != null && baseResponse.getData() == null) {
                rest.setMessage(baseResponse.getMsg());
                rest.setErrCode(baseResponse.getCode());
                return rest;
            }
        } catch (Exception e) {
            rest.setMessage(e.getMessage());
            rest.setErrCode("-1");
            return rest;
        }

        return null;
    }

    @Override
    public BaseResponse getQySfxx(AppQySfxxContentRequest appQySfxxContentRequest) throws Exception {
        String nsrsbh = appQySfxxContentRequest.getNsrsbh();
        Object sessionId = RedisUtil.get(RedisKeyConstant.redisKfptSessionKey + nsrsbh); //redis里取sessionId
        Object areaCode = RedisUtil.get(RedisKeyConstant.redisKfptAreaCodeKey + nsrsbh); //redis里取areacode
        Object gdslxDm = RedisUtil.get(RedisKeyConstant.redisKfptGdslxdmKey + nsrsbh); //redis里取gdslxdm
        Object clientNo = RedisUtil.get(RedisKeyConstant.redisKfptClientNoKey + nsrsbh);//redis里取客户端编码
        Object swjgdm = RedisUtil.get(RedisKeyConstant.redisKfptSwjgdmKey + nsrsbh);//redis里取税务机关代码
        KfptAppQySfxxQueryRequest kfptAppQySfxxQueryRequest = new KfptAppQySfxxQueryRequest();
        kfptAppQySfxxQueryRequest.setSessionId(sessionId == null ? null : sessionId.toString());
        kfptAppQySfxxQueryRequest.setAreaCode(areaCode == null ? appQySfxxContentRequest.getAreaCode() : areaCode.toString());
        kfptAppQySfxxQueryRequest.setGdslxDm(gdslxDm == null ? null : gdslxDm.toString());
        kfptAppQySfxxQueryRequest.setClientNo(clientNo == null ? null : clientNo.toString());
        kfptAppQySfxxQueryRequest.setSwjgDm(swjgdm == null ? null : swjgdm.toString());
        BeanUtils.copyProperties(appQySfxxContentRequest, kfptAppQySfxxQueryRequest);
        return nsrService.queryQySfxxExt(kfptAppQySfxxQueryRequest);
    }*/

//    @Override
//    public Rest<BaseResponse> nsrjbxxcx(BaseRequest request) throws Exception {
//        String nsrsbh = request.getShxydm();
//        KfptDoServiceRequest kfptDoServiceRequest = new KfptDoServiceRequest();
//        BeanUtils.copyProperties(request, kfptDoServiceRequest);
//        kfptDoServiceRequest.setNsrsbh(nsrsbh);
//        Rest<Boolean> booleanRest = getSessionService.LoginByNsrsbh(nsrsbh); // 登录
//        if (!booleanRest.getData()) {
//            throw new LoginException(booleanRest.getMessage());
//        }
//        bszsCommonUtils.setKfptDoServiceRequestFromRedis(kfptDoServiceRequest, nsrsbh);//设置redis
//        KfptAppQyxxQueryRequest kfptAppQyxxQueryRequest = new KfptAppQyxxQueryRequest();
//        BeanUtils.copyProperties(kfptDoServiceRequest, kfptAppQyxxQueryRequest);
//        return new Rest<>(nsrService.queryQyxxExt(kfptAppQyxxQueryRequest));
//    }
}